﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Helpers;
using System.Web.Http;
using System.Web.Script.Serialization;
using RBS.Core.Extension;
using RBS.Core.Model;
using RBS.Web.API.Models;

namespace RBS.Web.API.Areas.User.Controllers
{
    /// <summary>
    /// 食客登录
    /// </summary>
    public class LoginController : ApiController
    { 
        private readonly ApiContext _db = new ApiContext();  
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="user">用户实体</param>
        /// <returns>用户实体</returns> 
        public JsonData PostUser([FromBody] User user)
        {
            var data = new JsonData();
            try
            {
                var model = _db.U_User.FirstOrDefault(u => (u.U_Uid == user.Uid || u.Email==user.Uid || u.Mobile==user.Uid)  && u.PassWord == user.Pwd);
                if (model == null)
                {
                    data.Data = null;
                    data.Success = false;
                    data.Error = "用户名或密码不对！";
                    return data;
                }
                model.PassWord = "";
                data.Data =  model; 
                data.Success = true; 
            }
            catch (Exception exception)
            {
                data.Data =null;
                data.Success = false;
                data.Error = exception.Message;
            } 
            return data; 
        }

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    } 
    /// <summary>
    /// 用户登录实体
    /// </summary>
    public class User
    {
        /// <summary>
        /// 用户名
        /// </summary>
        public string Uid { get; set; }
        /// <summary>
        /// 用户密码
        /// </summary>
        public string Pwd { get; set; }
    }
}
